-- Guards will be found in the middle of the stage
-- They do not jump, only walk side to side looking for the hero
-- If they find the hero, the player loses and has to restart the game

guard = {
  x = 200,
  y = love.graphics.getHeight() -95 - (96/2),
  -- Sprite and Frame Variables
  spriteX = 70,
  spriteY = 97,
  frame = 0,
  frametime = 0,
  charQuads = {},
  charImage = love.graphics.newImage("images/sprites/guard.png"),
  -- Moving Variables
  speed = 30,
  direction = 2, -- Frontal Direction
  movetime = 0,

  loadImage = function()
    local count = 1  
    for j = 0, 3, 1 do
      for i = 0, 2, 1 do
        if(guard.charQuads[j] == nil) then 
          guard.charQuads[j] = {}
        end
        guard.charQuads[j][i] = love.graphics.newQuad(i * guard.spriteX, j * guard.spriteY, guard.spriteX, guard.spriteY, guard.charImage:getWidth(), guard.charImage:getHeight())
        count = count + 1
      end
    end
  end,
  

  animate = function(dt)
    guard.frametime = guard.frametime + dt
    if guard.frametime > 0.1 then      
      guard.frame = guard.frame + 1       
      guard.frametime = 0
    end    
    if guard.charQuads[guard.direction][guard.frame] == nil then
      guard.frame = 0
    end
  end,

  move = function(dt)  
  -- Moves Guard
  if guard.direction == 3 then --Left
    guard.x = guard.x - guard.speed * dt
  elseif guard.direction == 1 then --Right
    guard.x = guard.x + guard.speed * dt
  end  
  guard.movetime = guard.movetime + dt
  -- If time expires, changes guard direction to the other side
  if guard.movetime > 0.5 then
    guard.movetime = 0
    if guard.direction == 3 then --Left
      guard.direction = 1 --Change to Right
    elseif guard.direction == 1 then --Right
      guard.direction = 3  --Change to Left
    end  
  end
  guard.animate(dt) 
  end

}

--[[function guard:deepcopy(t, cache)
    if type(t) ~= 'table' then
        return t
    end

    cache = cache or {}
    if cache[t] then
        return cache[t]
    end

    local new = {}

    cache[t] = new

    for key, value in pairs(t) do
        new[guard:deepcopy(key, cache)] = guard:deepcopy(value, cache)
    end

    return new
end]]--